package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONObject
import com.alibaba.fastjson.parser.Feature
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: apiSyncDemo* @Description:系统API接口开发Demo ，重点关注业务逻辑部分
 * @author: LiJuntao* @date 2021-07-13 23:58:55
 *
 */

public class apiSyncParmaTest implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiSyncParmaTest.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 获取调用的Groovy
        String GroovyName = KHUtil.FastJsonNull(request.getParameter("type"));
        String appkey = KHUtil.FastJsonNull(request.getParameter("appkey"));
        log.debug("【获取调用的Groovy-】" + GroovyName);

        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String callJson, line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("【获取调用的参数报文-】" + callJson);

        JSONObject result = new JSONObject();

        if (KHUtil.isNull(GroovyName)) {
            result.put("code", -1);
            result.put("msg", "无调用类型!");
            return result.toString();
        } else {
            try {
                // 如果参数中传输appkey对请求重新签名一次
                if (KHUtil.isNull(appkey)) {
                    //业务逻辑部分 测试
                    JSONObject callJsono = JSONObject.parseObject(callJson, Feature.OrderedField);
                    long timestamp = System.currentTimeMillis()
                    String timestamps = String.valueOf(timestamp);
                    callJsono.put("access_key", appkey);
                    callJsono.put("nonce", KHUtil.getRandomString(8));
                    callJsono.put("timestamp", timestamps);

                    if (KHUtil.isNull(KHDBConfig.getSignInfo(eliteBean, appkey))) {

                        result.put("code", -2);
                        result.put("msg", "未获取到签名对应的秘钥请检查配置表!");
                        return result.toString();

                    } else {
                        String sign = signUtil.getSign(callJsono, appkey, KHDBConfig.getSignInfo(eliteBean, appkey))
                        callJsono.put("sign", sign);
                    }

                }

                // 发起调用
                String doURL = KHDBConfig.getAPIURL(eliteBean, "NGS-Local") + "/ngsapitask/api/"+GroovyName;

                return Util.doHttpPost(doURL,callJson,"UTF-8",10000,0);

            } catch (Exception e) {
                log.error("", e);

                result.put("code", -3);
                result.put("msg", "数据处理错误");

                return result.toString();
            }

        }
    }
}